﻿using System;
using System.Collections;
using System.ComponentModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace AuroraPlatform.View.Level3
{
    public class DataPagerProvider : IPagedCollectionView, INotifyPropertyChanged, IEnumerable
    {
        public bool CanChangePage
        {
            get { return this.PageIndex <= this.LastPageIndex && this.PageIndex >= 0; }
        }

        public bool IsPageChanging
        {
            get;
            private set;
        }

        public int ItemCount
        {
            get
            {
                return this.TotalItemCount;
            }
        }

        public bool MoveToFirstPage()
        {
            return this.MoveToPage(0);
        }

        public bool MoveToLastPage()
        {
            return this.MoveToPage(this.LastPageIndex);
        }

        public bool MoveToNextPage()
        {
            return this.MoveToPage(this.PageIndex + 1);
        }

        public bool MoveToPage(int pageIndex)
        {
            if (pageIndex < 0)
            {
                pageIndex = 0;
            }

            if (pageIndex > this.LastPageIndex)
            {
                pageIndex = this.LastPageIndex;
            }

            this.IsPageChanging = true;

            if (this.OnPageChanging(pageIndex))
            {
                return false;
            }

            this.PageIndex = pageIndex;

            this.IsPageChanging = false;

            this.OnPageChanged();

            return true;
        }

        public bool MoveToPreviousPage()
        {
            return this.MoveToPage(this.PageIndex - 1);
        }

        public event EventHandler<EventArgs> PageChanged;
        private void OnPageChanged()
        {
            if (this.PageChanged != null)
            {
                this.PageChanged(this, EventArgs.Empty);
            }
        }

        public void SetPaging(int totalItemCount, int pageSize, int pageIndex)
        {
            this.TotalItemCount = totalItemCount < 0 ? 0 : totalItemCount;
            this.PageSize = pageSize > 0 ? pageSize : 20;

            this.MoveToPage(pageIndex);
        }

        public event EventHandler<PageChangingEventArgs> PageChanging;

        private bool OnPageChanging(int newPageIndex)
        {
            if (this.PageChanging != null)
            {
                var e = new PageChangingEventArgs(newPageIndex);
                this.PageChanging(this, e);

                return e.Cancel;
            }

            return false;
        }

        private int _pageIndex = -1;
        public int PageIndex
        {
            get
            {
                return _pageIndex;
            }
            private set
            {
                if (_pageIndex != value)
                {
                    _pageIndex = value;

                    this.OnPropertyChanged("PageIndex");
                }
            }
        }

        private int _pageSize;

        public int PageSize
        {
            get
            {
                return _pageSize;
            }
            set
            {
                if (_pageSize != value)
                {
                    _pageSize = value;

                    this.OnPropertyChanged("PageSize");
                }
            }
        }

        private int _totalItemCount;

        public int TotalItemCount
        {
            get
            {
                return _totalItemCount;
            }
            private set
            {
                if (_totalItemCount != value)
                {
                    _totalItemCount = value;

                    this.OnPropertyChanged("TotalItemCount");
                    this.OnPropertyChanged("ItemCount");
                }
            }
        }


        private int LastPageIndex
        {
            get
            {
                if (this.PageSize > 0 && this.TotalItemCount > 0)
                {
                    return (int)Math.Ceiling((double)this.TotalItemCount / this.PageSize) - 1;
                }

                return 0;
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        public IEnumerator GetEnumerator()
        {
            throw new NotImplementedException();
        }
    }
}
